﻿using Microsoft.AspNetCore.Mvc;
using SaiLing.Common;
using SaiLing.Domain.Specification;
using SaiLing.SingleSignOn.Application.Core.Controllers;
using SaiLing.SingleSignOn.Application.Core.DataTransferObject.System;
using SaiLing.SingleSignOn.Application.Core.Params;
using SaiLing.SingleSignOn.Application.Service;
using SaiLing.SingleSignOn.Application.Service.Interfaces.System;
using SaiLing.SingleSignOn.Domain.Entities.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SaiLing.SingleSignOn.WebApi.Controllers
{
    public class PositionController : BaseController
    {
        private IPositionService PositionService { get; set; }

        public PositionController(IPositionService positionService)
        {
            this.PositionService = positionService;
        }

        [HttpGet]
        public async Task<ApiJsonResultData> GetByKey(Guid id)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                x.Data = await PositionService.GetByKey(id);
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> List(string name)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await PositionService.List(name);
                x.Data = list;
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> PageList(int pageIndex, int pageSize, string name)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await PositionService.PageList(pageIndex, pageSize, name);
                x.Data = list;
            });
        }

        public async Task<ApiJsonResultData> Create([FromForm]Position model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var result = await PositionService.Create(model);
                x.AddMessage("创建成功");
                x.Data = result;
                x.RedirectUrl = @return;
            });
        }

        public async Task<ApiJsonResultData> Edit([FromForm]Position model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var result = await PositionService.Edit(model);
                x.AddMessage("修改成功");
                x.Data = result;
                x.RedirectUrl = @return;
            });
        }

        [HttpPost]
        public async Task<ApiJsonResultData> Delete([FromBody]List<RemoveParam> model)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                if (model != null && model.Any())
                {
                    var list = new List<Position>();
                    model.ToList().ForEach(f =>
                    {
                        list.Add(new Position { Id = f.Id });
                    });
                    var rulst = await PositionService.Delete(list.ToArray());
                    x.ReloadPage = true;
                    x.Data = rulst;
                    x.AddMessage("删除成功");
                }
                else
                    x.AddErrorMessage("至少选择一条数据");
            });
        }
    }
}
